<?php
//时间戳格式一天为86400(60*60*24)
error_reporting(0);
ini_set('date.timezone','Asia/Shanghai');
header('Content-type: application/json');
	include_once '../config.php';
	include_once 'tools.php';
function curl_post_302($api_url,$data=null) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL, $api_url);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
	curl_setopt($ch, CURLOPT_TIMEOUT, 10);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, '');
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //返回数据写入内存不显示 
	$data = curl_exec($ch);
	$Headers = curl_getinfo($ch); 
	curl_close($ch);
	if ($data != $Headers){ 
	return $Headers["url"]; //返回的跳转URL
	}else{
	return false;
	}
}//获取跳转URL end
if(isset($_GET['action']) && $_GET['action'] == 'yes'){
	if(!$_GET['key'] == null && !$_GET['url'] == null){
		$key = addslashes($_GET['key']);
		$url = addslashes($_GET['url']);
		$time = new DateTime();
		$today = $time -> format("Y-m-d");
		$last_day = $time -> format("d");
		$today_stamp = new DateTime($today);
		$stamp = $today_stamp -> format('U');
		$resource=mysql_connect($host.":".$port,$username,$password);
		if (mysql_select_db($db,$resource)){
			if($resource){
				$sql = "SELECT * FROM `CDKey` WHERE `api_Key` = '$key'";
				$result=mysql_query($sql,$resource);
				if(mysql_num_rows($result) == 0){
					$arr = array(
						"action" => "1",
						"msg" => "404"
					);
					
					mysql_close($resource);
					exit (json_encode($arr));
				}
				else if(mysql_num_rows($result) == 1){
					$LongUrl = curl_post_302($url);
					if($LongUrl == null || $LongUrl == "" || $LongUrl == $url){
						$LongUrl = @file_get_contents("http://qiu.cn-wolf.cn/url_api.php?url=".$url);
					}
					$select_url_result = strpos($LongUrl,"battleofballs");
					if($select_url_result == false){
						$arr = array(
							"action" => "1",
							"msg" => "-3",
							"url" => $LongUrl
						);
					}
					else{
						$id = getSubstr($LongUrl,"?id=","&Account");
						$row = mysql_fetch_object($result);
						$Day = $row -> Day;
						
						$add_select_sql="SELECT * FROM `charge` WHERE `id` = '$id'";
						$add_select_result=mysql_query($add_select_sql,$resource);
						if(mysql_num_rows($add_select_result) == 1){
							$add_row_1 = mysql_fetch_object($add_select_result);
							$day_1 = new DateTime($add_row_1 -> duedate);
							$day_2 = null;
							if($day_1 -> format('U') - $stamp < 0 || $day_1 -> format('U') - $stamp == 0){
								$day_3 = new DateTime();
								$day_3 -> modify("+{$Day} day");
								if($day_3 -> format("Y") >= 9999){
									$day_2 = "9999-12-31";
								}
								else{
									$day_2 = $day_3 -> format("Y-m-d");
								}
							}
							else{
								$day_1 -> modify("+{$Day} day");
								if($day_1 -> format("Y") > 9999){
									$day_2 = "9999-12-31";
								}
								else{
									$day_2 = $day_1 -> format("Y-m-d");
								}
							}
							if($add_row_1 -> overdue == "stop"){
								$arr = array(
									"action" => "1",
									"type" => "2",
									"msg" => "stop"
								);
							}
							else{
								$res = get_id($id);
								$add_sql_1="UPDATE `charge` SET `CDkey` = '$key', `duedate` = '$day_2', `overdue` = 'no',`url` = '$url' WHERE `charge`.`id` = '$id'"; 
								$add_result=mysql_query($add_sql_1,$resource);
								if($add_result){
									$del_key_sql="DELETE FROM `CDKey` WHERE `CDKey`.`api_Key` = '$key'";
									$result_del_key=mysql_query($del_key_sql,$resource);
									if($result_del_key == true){
										$arr = array(
											"action" => "1",
											"msg" => "0",
											"type" => "2",
											"dateday" => $day_2,
											"result" => $res
										);
									}
									else{
										$arr = array(
											"action" => "1",
											"msg" => "1",
											"type" => "2",
											"dateday" => $day_2,
											"account" => "最后部署未完成"
										);
									}
								}
								else{
									$arr = array(
										"action" => "1",
										"msg" => "-1",
										"type" => "2"
									);
								}
							}
						}
						else if(mysql_num_rows($add_select_result) == 0){
							$res = get_id($id);
							$x = date("Y-m-d",$stamp + (86400 * (int)$Day));
							$add_sql="INSERT INTO `charge` (`id`, `CDkey`, `duedate`, `overdue`,`url`,`last_day`) VALUES ('$id', '$key', '$x', 'no','$url','$last_day')"; 
							$add_result_1=mysql_query($add_sql,$resource);
							if($add_result_1 == true){
								$del_key_sql="DELETE FROM `CDKey` WHERE `CDKey`.`api_Key` = '$key'";
								$result_del_key=mysql_query($del_key_sql,$resource);
								if($result_del_key == true){
									$arr = array(
										"action" => "1",
										"msg" => "0",
										"type" => "1",
										"dateday" => $Day,
										"result" => $res
									);
								}
								else{
									$arr = array(
										"action" => "1",
										"msg" => "1",
										"type" => "1",
										"account" => "最后部署未完成"
									);
								}
							}
							else{
								$arr = array(
									"action" => "1",
									"msg" => "-1",
									"type" => "1"
								);
							}
						}
						else{
							$arr = array(
								"action" => "1",
								"msg" => "-3000",
								"type" => "1"
							);
						}
					}
				}
				echo json_encode($arr);
				
				mysql_close($resource);
			}
		}
		else{
			$arr = array(
				"action" => "1",
				"msg" => "500"
			);
		}
	}
	else if(!$_GET['key'] == null && $_GET['url'] == null){
		$key = addslashes($_GET['key']);
		
		$resource=mysql_connect($host.":".$port,$username,$password);
		if (mysql_select_db($db,$resource)){
			if($resource){
				$sql="SELECT * FROM `charge` WHERE `CDkey` = '$key'";
				$result=mysql_query($sql,$resource);
				if($result == false){//查询失败
					$arr = array(
						"action" => "2",
						"msg" => "-3"
					);
				}
				else{
					if(mysql_num_rows($result) == 0){//没有查询到记录
						$arr = array(
							"action" => "2",
							"msg" => "404"
						);
					}
					else if(mysql_num_rows($result) == 1){
						$row = mysql_fetch_object($result);
						if($row -> overdue == 'yes'){
							$due = '已到期';
						}
						else if($row -> overdue == 'stop'){
							$due = '我们极力维护本平台成为一个对用户公平公正的平台<br />但由于种种原因，我们不得不对该ID停止服务<br />如果您是该ID的主人，请及时与我们联系解决问题。<br />为了用户的权益，我们一直在努力:)';
						}
						else{
							$due = $row -> duedate;
						}
						if($row -> url == null){
							$uurl_user = '暂无记录';
						}
						else{
							$uurl_url = $row -> url;
							$result_uurl = curl_post_302($uurl_url);
							if($result_uurl == null || $result_uurl == "" || $LongUrl == $uurl_url){
								$result_uurl = @file_get_contents("http://qiu.cn-wolf.cn/url_api.php?url=".$uurl_url);
							}
							$select_uurl = strpos($result_uurl,"battleofballs");
							if($select_uurl == false){
								$uurl_user = '暂无记录';
							}
							else{
								$result_uurl .= "'";
								$uurl_user = urldecode(getSubstr($result_uurl,"Account=","'"));
							}
						}
						if(isset($row -> last_day) && isset($row -> overdue)){
							if($row -> overdue == "no"){
								if((int)$row -> last_day == (int)$last_day){
									$server = "<b style='color: green;'>今日已完成</b>";
								}
								else{
									$server = "<b style='color: orange;'>正在任务队列中</b>";
								}
							}
							else if($row -> overdue == "yes"){
								$server = "<b style='color: orange;'>已到期</b>";
							}
							else if($row -> overdue == "stop"){
								$server = "<b style='color: orange;'>已封停</b>";
							}
						}
						else{
							$server = "<b style='color: red;'>未知</b>";
						}
						$arr = array(
							"action" => "2",
							"msg" => "0",
							"username" => $uurl_user,
							"url" => $row -> url,
							"cdk" => $row -> CDkey,
							"datetime" => $due,
							"server" => $server
						);
					}
					else{//数据异常
						$arr = array(
							"action" => "2",
							"msg" => "-3000"
						);
					}
				}	
				mysql_close($resource);
				
				echo json_encode($arr);
			}
		}
		else{//连接数据库失败
			$arr = array(
				"action" => "2",
				"msg" => "500"
			);
		}
	}
	else if($_GET['key'] == null && !$_GET['url'] == null){
		$url = addslashes($_GET['url']);
		
		$LongUrl = curl_post_302($url);
		if($LongUrl == null || $LongUrl == "" || $LongUrl == $url){
			$LongUrl = @file_get_contents("http://qiu.cn-wolf.cn/url_api.php?url=".$url);
		}
			$select_url_result = strpos($LongUrl,"battleofballs");
			if($select_url_result == false){//未查询到id
				$arr = array(
					"action" => "3",
					"msg" => "-1",
					"url" => $LongUrl
				);
				
				mysql_close($resource);
				exit (json_encode($arr));
			}
			else{
				$id = getSubstr($LongUrl,"?id=","&Account");
			}
		$resource=mysql_connect($host.":".$port,$username,$password);
		if (mysql_select_db($db,$resource)){
			if($resource){
				$sql="SELECT * FROM `charge` WHERE `id` = '$id'";
				$result=mysql_query($sql,$resource);
				if($result == false){//查询失败
					$arr = array(
						"action" => "3",
						"msg" => "-3",
						"url" => $LongUrl
					);
				}
				else{
					if(mysql_num_rows($result) == 0){
						$arr = array(
							"action" => "3",
							"msg" => "404"
						);
					}
					else if(mysql_num_rows($result) == 1){
						$row = mysql_fetch_object($result);
						if($row -> overdue == 'yes'){
							$due = '已到期';
						}
						else if($row -> overdue == 'stop'){
							$due = '我们极力维护本平台成为一个对用户公平公正的平台<br />但由于种种原因，我们不得不对该ID停止服务<br />如果您是该ID的主人，请及时与我们联系解决问题。<br />为了用户的权益，我们一直在努力:)';
						}
						else{
							$due = $row -> duedate;
						}
						if($row -> url == null){
							$uurl_url = $url;
							$result_uurl = curl_post_302($uurl_url);
							if($result_uurl == null || $result_uurl == "" || $LongUrl == $uurl_url){
								$result_uurl = @file_get_contents("http://qiu.cn-wolf.cn/url_api.php?url=".$uurl_url);
							}
							$select_uurl = strpos($result_uurl,"battleofballs");
							if($select_uurl == false){
								$uurl_user = '暂无记录';
							}
							else{
								$result_uurl .= "'";
								$uurl_user = urldecode(getSubstr($result_uurl,"Account=","'"));
							}
						}
						else{
							$uurl_url = $row -> url;
							$result_uurl = curl_post_302($uurl_url);
							if($result_uurl == null || $result_uurl == "" || $LongUrl == $uurl_url){
								$result_uurl = @file_get_contents("http://qiu.cn-wolf.cn/url_api.php?url=".$uurl_url);
							}
							$select_uurl = strpos($result_uurl,"battleofballs");
							if($select_uurl == false){
								$uurl_user = '暂无记录';
							}
							else{
								$result_uurl .= "'";
								$uurl_user = urldecode(getSubstr($result_uurl,"Account=","'"));
							}
						}
						if(isset($row -> last_day)){
							$last = new DateTime();
							if($row -> last_day == $last -> format("j")){
								$server = "<b style='color: green;'>今日已完成</b>";
							}
							else{
								$server = "<b style='color: orange;'>正在任务队列中</b>";
							}
						}
						else{
							$server = "<b style='color: red;'>未知</b>";
						}
						$arr = array(
							"action" => "3",
							"msg" => "0",
							"username" => $uurl_user,
							"url" => $url,
							"cdk" => $row -> CDkey,
							"datetime" => $due,
							"server" => $server
						);
					}
					else{//数据库异常
						$arr = array(
							"action" => "3",
							"msg" => "-3000"
						);
					}
				}	
				echo json_encode($arr);
				
				mysql_close($resource);
			}
			else{//连接数据库失败
				$arr = array(
					"action" => "2",
					"msg" => "500"
				);
			}
		}
	}
}
?>